<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/5/24 0024
 * Time: 16:59
 */

namespace XtwsBack\Controller;


use Think\Controller;

class ForetasteOrderController extends Controller
{
    public function index(){

        $order = M('foretaste_order');
        $count = $order->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $list = $order->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach($list as $k => $v) {// 查询拼团商品信息，拼团的商家或者农户的信息
            $list[$k]['nickname'] = M('member')->where('id=' . $v['u_id'])->getField('nickname');
            //$list[$k]['goods_info'] 根据sg_id查询拼团商品信息
            $list[$k]['goods'] = M('foretaste_goods')->where('id=' . $v['g_id'])->field('category,title,h_imgs')->find();
            // 处理主图
            $list[$k]['goods']['h_imgs'] = isMorePic($list[$k]['goods']['h_imgs']);
            switch ($v['state']){
                case 0:
                case 7:
                    $list[$k]['state'] = '申请中';
                    break;
                case 1:
                    $list[$k]['state'] = '申请成功';
                    break;
                case 2:
                    $list[$k]['state'] = '已发货';
                    break;
                case 3:
                    $list[$k]['state'] = '待核销';
                    break;
                case 4:
                    $list[$k]['state'] = '待评价';
                    break;
                case 5:
                    $list[$k]['state'] = '已评价';
                    break;
                case 6:
                    $list[$k]['state'] = '失败';
                    break;
            }
        }//end foreach

//        dump($list);
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出

        //加载视图
        $this->display();
    }//end fun index()


    // 订单详情
    public function orderinfo(){
        $id = intval(I('id'));
        $info = getInfo('foretaste_order',['id'=>$id]);
        if($info){
            $member =  getMember($info['u_id'],$field='nickname,avatar,realname,mobile');
            // 根据g_id查询拼团商品信息
            $goods = getInfo('foretaste_goods',array('id'=>$info['g_id']),'u_id,category,title,h_imgs,price');
            $goods['h_imgs'] = explode(",",$goods['h_imgs']);
            // 查询发布人的头像、昵称
            $goods['member'] = getMember($goods['u_id'],'nickname,avatar,c_id');


            // 获取订单地址
            $addr_info = M('shop_community')->where(array('id' =>  $goods['member']['c_id'] ))->field('title,a_id_1,a_id_2,a_id_3')->find();
            $addr_info['pro'] = M('area')->where('id='.$addr_info['a_id_1'])->getField('name');
            $addr_info['city'] = M('area')->where('id='.$addr_info['a_id_2'])->getField('name');
            $addr_info['area'] = M('area')->where('id='.$addr_info['a_id_3'])->getField('name');
            $addr_info['store'] = M('shop_store')->where('c_id='.$goods['member']['c_id'])->getField('storename');

            $this -> assign('info',$info);              // 订单信息
            $this -> assign('member',$member);          // 购买人
            $this -> assign('goods',$goods);            // 活动商品信息
            $this -> assign('addr_info',$addr_info);    // 地址
            $this->display();
        }else{
            $this->error('没有记录');
        }// end if

    }// end fun orderinfo()

    //发货
    public function deliver(){
        $id = $_GET['oid'];
        if (!empty($id)){
            $info = M('foretaste_order')->where("id = {$id}")->find();
            if ($info['state'] == 1){
                $order = M('foretaste_order')->where("id = {$id}")->save(['state'=>2]);
                if ($order){
                    M("voice")->add(['u_id'=>$info['u_id'],'content'=>'您参与试吃活动获得的产品已发货了！','sendtime'=>time()]);
                    $this->success('发货成功');
                    return;
                }else{
                    $this->error('发货失败');
                }
            }//end if
        }//end if
    }//end fun deliver()

    //到货通知
    public function advising(){
        $id = $_GET['oid'];
        if (!empty($id)){
            $info = M('foretaste_order')->where("id = {$id}")->find();
            if ($info['state'] == 2){
                $order = M('foretaste_order')->where("id = {$id}")->save(['state'=>3]);
                if ($order){
                    $goods_info = M('foretaste_goods')->where("id = {$info['g_id']}")->find();
                    $member_info = getMember($info['u_id'],'c_id');
                    $store_info = M("shop_store")->where("c_id = {$member_info['c_id']}")->field('storename,address,tel')->find();
                    D("Wx/SendMsg")->arrivalmsg($info['u_id'],$goods_info['title'],$info['order_sn'],$store_info['address'].$store_info['storename'],$store_info['tel'],"http://www.xiangtws.com/index.php/Wx/ForetasteOrder/order/oid/".$info['id']);
                    M("voice")->add(['u_id'=>$info['u_id'],'content'=>'您的试吃产品已到货了！','sendtime'=>time()]);
                    $this->success('通知成功');
                    return;
                }else{
                    $this->error('通知失败');
                }
            }//end if
        }//end if
    }//end fun advising()
}//end class